package cn.b.sky.mes.andon.controller;

import java.io.UnsupportedEncodingException;
import java.net.URLDecoder;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.HashMap;
import java.util.LinkedList;
import java.util.List;
import java.util.Map;

import javax.servlet.http.HttpServletRequest;

import lombok.extern.slf4j.Slf4j;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.servlet.ModelAndView;

import com.alibaba.fastjson.JSON;

import cn.b.sky.mes.andon.model.Andon;
import cn.b.sky.mes.andon.model.AndonGroup;
import cn.b.sky.mes.andon.model.AndonGroupAssign;
import cn.b.sky.mes.andon.model.AndonTemplate;
import cn.b.sky.mes.andon.model.condition.AndonCondition;
import cn.b.sky.mes.andon.model.condition.AndonTemplateCondition;
import cn.b.sky.mes.andon.model.condition.AndonTypeCondition;
import cn.b.sky.mes.andon.service.AndonGroupAssignService;
import cn.b.sky.mes.andon.service.AndonGroupService;
import cn.b.sky.mes.andon.service.AndonService;
import cn.b.sky.mes.andon.service.AndonTemplateService;
import cn.b.sky.mes.andon.service.AndonTypeService;
import cn.b.sky.mes.cm.model.WorkCenter;
import cn.b.sky.mes.cm.model.WorkUnit;
import cn.b.sky.mes.cm.service.WorkCenterService;
import cn.b.sky.mes.cm.service.WorkUnitService;
import cn.b.sky.common.model.BatchJobReport;
import cn.b.sky.common.model.DWZResult;
import cn.b.sky.user.controller.SecurityController;

/**
 * 
 * <strong>Title : AndonController.java<br></strong>
 * <strong>Package : cn.b.sky.mes.andon.controller<br></strong>
 * <strong>Description : </strong>按灯站点控制器<br> 
 * <strong>Create on : 2016年10月14日 下午1:13:43<br></strong>
 * <p>
 * @author dapeng.li<br>
 * @version <strong>v1.0.0</strong><br>
 * <br>
 * <strong>修改历史:</strong><br>
 * 修改人|修改日期|修改描述<br>
 * -------------------------------------------<br>
 * <br>
 * <br>
 */
@Controller
@RequestMapping("/andon/*")
@Slf4j
public class AndonController extends SecurityController {
  @Autowired
  private AndonService andonService;

  @Autowired
  private AndonGroupService andonGroupService;

  @Autowired
  private AndonGroupAssignService andonGroupAssignService;

  @Autowired
  private WorkCenterService workCenterService;

  @Autowired
  private WorkUnitService workUnitService;

  @Autowired
  private AndonTemplateService andonTemplateService;

  @Autowired
  private AndonTypeService andonTypeService;

  @RequestMapping("andonPage")
  public String andonPage(Model model, String navTabId) {
    model.addAttribute(NAVTABID, navTabId);
    model.addAttribute("workCentersTree", getWorkCenterTree());
    // ANDON模板
    AndonTemplateCondition condition = new AndonTemplateCondition();
    condition.setSiteCode(getSiteCode());
    condition.setLockFlag(0);
    List<AndonTemplate> templates = andonTemplateService.selectAndonTemplatesByCondition(condition);
    model.addAttribute("templates", templates);
    // ANDON类型
    AndonTypeCondition andonTypeCondition = new AndonTypeCondition();
    andonTypeCondition.setSiteCode(this.getSiteCode());
    andonTypeCondition.setLockFlag(0);
    model.addAttribute("andonTypes", andonTypeService.getAllAndonTypes(getSiteCode()));
    return "andon/andon_list";
  }

  /**
   * 
   * <strong>Title : reloadTree<br></strong>
   * <strong>Description : </strong>局部刷新树组件<br> 
   * <strong>Create on : 2016年10月18日 下午4:50:46<br></strong>
   * <p>
   * @param m
   * @return
   * ModelAndView
   * @throws 
   * @author dapeng.li<br>
   * @version <strong>v1.0.0</strong><br>
   * <br>
   * <strong>修改历史:</strong><br>
   * 修改人 | 修改日期 | 修改描述<br>
   * -------------------------------------------<br>
   * <br>
   * <br>
   */
  @RequestMapping("reloadTree")
  @ResponseBody
  public ModelAndView reloadTree(ModelAndView m) {
    m.addObject("workCentersTree", getWorkCenterTree());
    m.setViewName("andon/tree");
    return m;
  }

  /**
   * 
   * <strong>Title : andonLookUp<br></strong>
   * <strong>Description : </strong>ANDON查找带回界面<br> 
   * <strong>Create on : 2016年10月19日 上午9:14:59<br></strong>
   * <p>
   * @param model
   * @param navTabId
   * @return
   * String
   * @throws 
   * @author JunLin.Yang<br>
   * @version <strong>v1.0.0</strong><br>
   * <br>
   * <strong>修改历史:</strong><br>
   * 修改人 | 修改日期 | 修改描述<br>
   * -------------------------------------------<br>
   * <br>
   * <br>
   */
  @RequestMapping("andonLookUp")
  public String andonLookUp(Model model, String navTabId) {
    model.addAttribute(NAVTABID, navTabId);
    model.addAttribute("workCentersTree", getWorkCenterTree());
    // ANDON类型
    AndonTypeCondition andonTypeCondition = new AndonTypeCondition();
    andonTypeCondition.setSiteCode(this.getSiteCode());
    andonTypeCondition.setLockFlag(0);
    model.addAttribute("andonTypes", andonTypeService.getAllAndonTypes(getSiteCode()));
    return "andon/andon_lookup";
  }

  /**
   * 
   * <strong>Title : getWorkCenterTree<br></strong>
   * <strong>Description : </strong>组装工作中心、作业单元、按灯组树结构<br> 
   * <strong>Create on : 2016年10月19日 上午9:13:00<br></strong>
   * <p>
   * @return
   * List<WorkCenter>
   * @throws 
   * @author dapeng.li<br>
   * @version <strong>v1.0.0</strong><br>
   * <br>
   * <strong>修改历史:</strong><br>
   * 修改人 | 修改日期 | 修改描述<br>
   * -------------------------------------------<br>
   * <br>
   * <br>
   */
  private List<WorkCenter> getWorkCenterTree() {
    // 组装工作中心
    List<WorkCenter> workCenters = workCenterService.getAllWorkCenterListBySiteCode(getSiteCode(), 0);
    Map<String, List<WorkCenter>> centerMap = new HashMap<String, List<WorkCenter>>();
    centerMap.put("", new LinkedList<WorkCenter>());
    for (WorkCenter workCenter : workCenters) {
      if (StringUtils.isEmpty(workCenter.getParentWorkCenterCode())) {
        centerMap.get("").add(workCenter);
      } else {
        if (!centerMap.containsKey(workCenter.getParentWorkCenterCode())) {
          centerMap.put(workCenter.getParentWorkCenterCode(), new LinkedList<WorkCenter>());
        }
        centerMap.get(workCenter.getParentWorkCenterCode()).add(workCenter);
      }
    }
    // 组装作业单元
    List<WorkUnit> workUnits = workUnitService.getAllWorkUnitListBySiteCode(getSiteCode(), 0);
    Map<String, List<WorkUnit>> centerUnitMap = new HashMap<String, List<WorkUnit>>();
    for (WorkUnit workUnit : workUnits) {
      if (!centerUnitMap.containsKey(workUnit.getWorkCenterCode())) {
        centerUnitMap.put(workUnit.getWorkCenterCode(), new LinkedList<WorkUnit>());
      }
      centerUnitMap.get(workUnit.getWorkCenterCode()).add(workUnit);
    }

    // 组装按灯组
    List<AndonGroup> andonGroups = andonGroupService.getAllAndonGroups(getSiteCode());
    Map<String, List<AndonGroup>> andonGroupMap = new HashMap<String, List<AndonGroup>>();
    for (AndonGroup group : andonGroups) {
      if (!andonGroupMap.containsKey(group.getWorkUnitCode())) {
        andonGroupMap.put(group.getWorkUnitCode(), new LinkedList<AndonGroup>());
      }
      andonGroupMap.get(group.getWorkUnitCode()).add(group);
    }
    setAndonGroups(workUnits, andonGroupMap);

    List<WorkCenter> workCentersTree = centerMap.get("");
    setChildrenNodes(centerMap, centerUnitMap, workCentersTree);
    return workCentersTree;
  }

  /**
   * 
   * <strong>Title : setChildrenNodes<br></strong>
   * <strong>Description : </strong>设置工作中心作业单元树结构<br> 
   * <strong>Create on : 2016年9月10日 上午11:02:03<br></strong>
   * <p>
   * @param centerMap
   * @param centerUnitMap
   * @param workCenters
   * void
   * @throws 
   * @author dapeng.li<br>
   * @version <strong>v1.0.0</strong><br>
   * <br>
   * <strong>修改历史:</strong><br>
   * 修改人 | 修改日期 | 修改描述<br>
   * -------------------------------------------<br>
   * <br>
   * <br>
   */
  private void setChildrenNodes(Map<String, List<WorkCenter>> centerMap, Map<String, List<WorkUnit>> centerUnitMap,
      List<WorkCenter> workCenters) {
    for (WorkCenter workCenter : workCenters) {
      if (centerMap.containsKey(workCenter.getWorkCenterCode())) {
        workCenter.setChildrens(centerMap.get(workCenter.getWorkCenterCode()));
        setChildrenNodes(centerMap, centerUnitMap, workCenter.getChildrens());
      }
      if (centerUnitMap.containsKey(workCenter.getWorkCenterCode())) {
        workCenter.setWorkUnits(centerUnitMap.get(workCenter.getWorkCenterCode()));
      }
    }
  }

  /**
   * 
   * <strong>Title : setAndonGroups<br></strong>
   * <strong>Description : </strong>将按灯组挂在作业单元下<br> 
   * <strong>Create on : 2016年10月17日 下午1:56:59<br></strong>
   * <p>
   * @param workUnits
   * @param andonGroupMap
   * void
   * @throws 
   * @author dapeng.li<br>
   * @version <strong>v1.0.0</strong><br>
   * <br>
   * <strong>修改历史:</strong><br>
   * 修改人 | 修改日期 | 修改描述<br>
   * -------------------------------------------<br>
   * <br>
   * <br>
   */
  private void setAndonGroups(List<WorkUnit> workUnits, Map<String, List<AndonGroup>> andonGroupMap) {
    for (WorkUnit workUnit : workUnits) {
      if (andonGroupMap.containsKey(workUnit.getWorkUnitCode())) {
        workUnit.setAndongroups(andonGroupMap.get(workUnit.getWorkUnitCode()));
      }
    }
  }

  /**
   * 
   * <strong>Title : getAndonListByCondition<br></strong>
   * <strong>Description : </strong>数据分页查询<br> 
   * <strong>Create on : 2016年10月19日 上午9:13:38<br></strong>
   * <p>
   * @param condition
   * @return
   * DWZResult
   * @throws 
   * @author dapeng.li<br>
   * @version <strong>v1.0.0</strong><br>
   * <br>
   * <strong>修改历史:</strong><br>
   * 修改人 | 修改日期 | 修改描述<br>
   * -------------------------------------------<br>
   * <br>
   * <br>
   */
  @RequestMapping("getAndonListByCondition")
  @ResponseBody
  public DWZResult getAndonListByCondition(AndonCondition condition) {
    if (StringUtils.isEmpty(condition.getSiteCode())) {
      condition.setSiteCode(getSiteCode());
    }
    condition.setData(andonService.getAndonListByCondition(condition));
    condition.setTotalCount(andonService.getAndonCountByCondition(condition));
    return ajaxDone(condition);
  }

  /**
   * 
   * <strong>Title : validateAndonGroup<br></strong>
   * <strong>Description : </strong>检验按灯组不存在<br> 
   * <strong>Create on : 2016年10月19日 上午9:13:57<br></strong>
   * <p>
   * @param group
   * @return
   * boolean
   * @throws 
   * @author dapeng.li<br>
   * @version <strong>v1.0.0</strong><br>
   * <br>
   * <strong>修改历史:</strong><br>
   * 修改人 | 修改日期 | 修改描述<br>
   * -------------------------------------------<br>
   * <br>
   * <br>
   */
  @RequestMapping("validateAndonGroup")
  @ResponseBody
  public boolean validateAndonGroup(AndonGroup group) {
    if (StringUtils.isEmpty(group.getSiteCode())) {
      group.setSiteCode(getSiteCode());
    }
    return null == andonGroupService.getAndonGroupByName(getSiteCode(), group.getWorkUnitCode(), group.getAndonGroup());
  }

  /**
   * 
   * <strong>Title : addAndonGroup<br></strong>
   * <strong>Description : </strong>新增按灯组<br> 
   * <strong>Create on : 2016年10月19日 上午9:14:14<br></strong>
   * <p>
   * @param group
   * @return
   * DWZResult
   * @throws 
   * @author dapeng.li<br>
   * @version <strong>v1.0.0</strong><br>
   * <br>
   * <strong>修改历史:</strong><br>
   * 修改人 | 修改日期 | 修改描述<br>
   * -------------------------------------------<br>
   * <br>
   * <br>
   */
  @RequestMapping("addAndonGroup")
  @ResponseBody
  public DWZResult addAndonGroup(AndonGroup group) {
    if (StringUtils.isEmpty(group.getSiteCode())) {
      group.setSiteCode(getSiteCode());
    }
    andonGroupService.insertAndonGroup(group);
    return ajaxDone(group);
  }

  /**
   * 
   * <strong>Title : renameAndonGroup<br></strong>
   * <strong>Description : </strong>重命名按灯组<br> 
   * <strong>Create on : 2016年10月19日 上午9:14:27<br></strong>
   * <p>
   * @param group
   * @return
   * DWZResult
   * @throws 
   * @author dapeng.li<br>
   * @version <strong>v1.0.0</strong><br>
   * <br>
   * <strong>修改历史:</strong><br>
   * 修改人 | 修改日期 | 修改描述<br>
   * -------------------------------------------<br>
   * <br>
   * <br>
   */
  @RequestMapping("renameAndonGroup")
  @ResponseBody
  public DWZResult renameAndonGroup(AndonGroup group) {
    if (StringUtils.isEmpty(group.getSiteCode())) {
      group.setSiteCode(getSiteCode());
    }
    andonGroupService.updateAndonGroup(group);
    return ajaxDone(group);
  }

  /**
   * 
   * <strong>Title : deleteAndonGroup<br></strong>
   * <strong>Description : </strong>删除按灯组<br> 
   * <strong>Create on : 2016年10月19日 上午9:14:39<br></strong>
   * <p>
   * @param group
   * @return
   * DWZResult
   * @throws 
   * @author dapeng.li<br>
   * @version <strong>v1.0.0</strong><br>
   * <br>
   * <strong>修改历史:</strong><br>
   * 修改人 | 修改日期 | 修改描述<br>
   * -------------------------------------------<br>
   * <br>
   * <br>
   */
  @RequestMapping("deleteAndonGroup")
  @ResponseBody
  public DWZResult deleteAndonGroup(AndonGroup group) {
    if (StringUtils.isEmpty(group.getSiteCode())) {
      group.setSiteCode(getSiteCode());
    }
    andonGroupService.deleteAndonGroup(group);
    return ajaxDone(group);
  }

  /**
   * 
   * <strong>Title : addAndonPage<br></strong>
   * <strong>Description : </strong>新增界面<br> 
   * <strong>Create on : 2016年10月19日 上午9:18:21<br></strong>
   * <p>
   * @param model
   * @return
   * String
   * @throws 
   * @author dapeng.li<br>
   * @version <strong>v1.0.0</strong><br>
   * <br>
   * <strong>修改历史:</strong><br>
   * 修改人 | 修改日期 | 修改描述<br>
   * -------------------------------------------<br>
   * <br>
   * <br>
   */
  @RequestMapping("addAndonPage")
  public String addAndonPage(Model model, String workUnitCode, String andonGroup) {
    model.addAttribute("workUnitCode", workUnitCode);
    model.addAttribute("andonGroup", andonGroup);
    AndonTemplateCondition condition = new AndonTemplateCondition();
    condition.setSiteCode(getSiteCode());
    condition.setLockFlag(0);
    List<AndonTemplate> templates = andonTemplateService.selectAndonTemplatesByCondition(condition);
    model.addAttribute("templates", templates);
    model.addAttribute(PATTERN, "add");
    return "andon/andonPage";
  }

  /**
   * 
   * <strong>Title : editAndonPage<br></strong>
   * <strong>Description : </strong>编辑页面<br> 
   * <strong>Create on : 2016年10月19日 下午3:26:26<br></strong>
   * <p>
   * @param model
   * @param workUnitCode
   * @param andonCode
   * @return
   * String
   * @throws 
   * @author dapeng.li<br>
   * @version <strong>v1.0.0</strong><br>
   * <br>
   * <strong>修改历史:</strong><br>
   * 修改人 | 修改日期 | 修改描述<br>
   * -------------------------------------------<br>
   * <br>
   * <br>
   */
  @RequestMapping("editAndonPage")
  public String editAndonPage(Model model, String workUnitCode, String andonCode) {
    model.addAttribute("workUnitCode", workUnitCode);
    Andon andon = andonService.getAndonByCode(getSiteCode(), workUnitCode, andonCode);
    model.addAttribute("andon", andon);
    AndonTemplateCondition condition = new AndonTemplateCondition();
    condition.setSiteCode(getSiteCode());
    condition.setLockFlag(0);
    List<AndonTemplate> templates = andonTemplateService.selectAndonTemplatesByCondition(condition);
    model.addAttribute("templates", templates);
    model.addAttribute(PATTERN, "edit");
    model.addAttribute("isUpdate", true);
    return "andon/andonPage";
  }

  /**
   * 
   * <strong>Title : viewAndonPage<br></strong>
   * <strong>Description : </strong>ANDON详情页<br> 
   * <strong>Create on : 2016年10月19日 下午3:27:18<br></strong>
   * <p>
   * @param model
   * @param workUnitCode
   * @param andonCode
   * @return
   * String
   * @throws 
   * @author dapeng.li<br>
   * @version <strong>v1.0.0</strong><br>
   * <br>
   * <strong>修改历史:</strong><br>
   * 修改人 | 修改日期 | 修改描述<br>
   * -------------------------------------------<br>
   * <br>
   * <br>
   */
  @RequestMapping("viewAndonPage")
  public String viewAndonPage(Model model, String workUnitCode, String andonCode) {
    model.addAttribute("workUnitCode", workUnitCode);
    Andon andon = andonService.getAndonByCode(getSiteCode(), workUnitCode, andonCode);
    model.addAttribute("andon", andon);
    AndonTemplateCondition condition = new AndonTemplateCondition();
    condition.setSiteCode(getSiteCode());
    condition.setLockFlag(0);
    List<AndonTemplate> templates = andonTemplateService.selectAndonTemplatesByCondition(condition);
    model.addAttribute("templates", templates);
    model.addAttribute(PATTERN, "view");
    return "andon/andonPage";
  }

  /**
   * 
   * <strong>Title : getAndonTemplateDetail<br></strong>
   * <strong>Description : </strong>获取ANDON模板相信信息<br> 
   * <strong>Create on : 2016年10月19日 下午2:37:50<br></strong>
   * <p>
   * @param templateCode
   * @return
   * AndonTemplate
   * @throws 
   * @author dapeng.li<br>
   * @version <strong>v1.0.0</strong><br>
   * <br>
   * <strong>修改历史:</strong><br>
   * 修改人 | 修改日期 | 修改描述<br>
   * -------------------------------------------<br>
   * <br>
   * <br>
   */
  @RequestMapping("getAndonTemplateDetail")
  @ResponseBody
  public AndonTemplate getAndonTemplateDetail(String templateCode) {
    return andonTemplateService.getAndonTemplateDetail(getSiteCode(), templateCode);
  }

  /**
   * 
   * <strong>Title : validateAndonCode<br></strong>
   * <strong>Description : </strong>检验andon编码是否存在<br> 
   * <strong>Create on : 2016年10月20日 上午9:25:41<br></strong>
   * <p>
   * @param workUnitCode
   * @param andonCode
   * @return
   * boolean
   * @throws 
   * @author dapeng.li<br>
   * @version <strong>v1.0.0</strong><br>
   * <br>
   * <strong>修改历史:</strong><br>
   * 修改人 | 修改日期 | 修改描述<br>
   * -------------------------------------------<br>
   * <br>
   * <br>
   */
  @RequestMapping("validateAndonCode")
  @ResponseBody
  public boolean validateAndonCode(String workUnitCode, String andonCode) {
    if (StringUtils.isEmpty(workUnitCode) || StringUtils.isEmpty(andonCode)) {
      return true;
    } else {
      return null == andonService.getAndonByCode(getSiteCode(), workUnitCode.toUpperCase(), andonCode.toUpperCase());
    }
  }

  /**
   * 
   * <strong>Title : saveAndon<br></strong>
   * <strong>Description : </strong>保存ANDON数据<br> 
   * <strong>Create on : 2016年10月19日 下午3:24:21<br></strong>
   * <p>
   * @param andon
   * @param isUpdate
   * @return
   * DWZResult
   * @throws 
   * @author dapeng.li<br>
   * @version <strong>v1.0.0</strong><br>
   * <br>
   * <strong>修改历史:</strong><br>
   * 修改人 | 修改日期 | 修改描述<br>
   * -------------------------------------------<br>
   * <br>
   * <br>
   */
  @RequestMapping("saveAndon")
  @ResponseBody
  public DWZResult saveAndon(Andon andon, Boolean isUpdate, String group) {
    andon.setSiteCode(getSiteCode());
    if (null == andon.getLockFlag()) {
      andon.setLockFlag(0);
    }
    setCreateInfo(andon);
    // 在组下创建andon时，解析特殊字符组名
    if (!StringUtils.isEmpty(group)) {
      try {
        andon.setAndonGroup(URLDecoder.decode(group, "UTF-8"));
      } catch (UnsupportedEncodingException e) {
        log.error("{}", e);
      }
    }
    if (null != isUpdate && isUpdate.booleanValue()) {
      andonService.updateAndon(andon);
    } else {
      andonService.insertAndon(andon);
      if (!StringUtils.isEmpty(andon.getAndonGroup())) {
        AndonGroupAssign andonGroupAssign = new AndonGroupAssign();
        andonGroupAssign.setAndonCode(andon.getAndonCode());
        andonGroupAssign.setAndonGroup(andon.getAndonGroup());
        andonGroupAssign.setWorkUnitCode(andon.getWorkUnitCode());
        andonGroupAssign.setSiteCode(getSiteCode());
        andonGroupAssignService.insertAndonGroupAssign(andonGroupAssign);
      }
    }
    return ajaxDone(null);
  }

  /**
   * 
   * <strong>Title : deleteAndon<br></strong>
   * <strong>Description : </strong>删除ANDON数据<br> 
   * <strong>Create on : 2016年10月20日 下午2:59:51<br></strong>
   * <p>
   * @param request
   * @param ids
   * @return
   * DWZResult
   * @throws 
   * @author dapeng.li<br>
   * @version <strong>v1.0.0</strong><br>
   * <br>
   * <strong>修改历史:</strong><br>
   * 修改人 | 修改日期 | 修改描述<br>
   * -------------------------------------------<br>
   * <br>
   * <br>
   */
  @RequestMapping("deleteAndon")
  @ResponseBody
  public DWZResult deleteAndon(HttpServletRequest request, String[] ids) {
    BatchJobReport[] reports = new BatchJobReport[ids.length];
    int failures = 0;
    for (int i = 0; i < ids.length; i++) {
      String[] keys = ids[i].split(";");
      reports[i] = andonService.deleteAndon(getSiteCode(), keys[0], keys[1]);
      if (!reports[i].isSuccess()) {
        failures += 1;
      }
    }
    if (failures == 0) {
      return ajaxDone(null);
    } else {
      return batchjobAjaxDone(request, Arrays.asList(reports));
    }
  }

  /**
   * 
   * <strong>Title : assignAndonGroup<br></strong>
   * <strong>Description : </strong>ANDON数据分组<br> 
   * <strong>Create on : 2016年10月20日 下午3:00:04<br></strong>
   * <p>
   * @param workUnitCode
   * @param groupos
   * @param andons
   * @return
   * DWZResult
   * @throws 
   * @author dapeng.li<br>
   * @version <strong>v1.0.0</strong><br>
   * <br>
   * <strong>修改历史:</strong><br>
   * 修改人 | 修改日期 | 修改描述<br>
   * -------------------------------------------<br>
   * <br>
   * <br>
   */
  @RequestMapping("assignAndonGroup")
  @ResponseBody
  public DWZResult assignAndonGroup(String workUnitCode, String[] groupos, String andons) {
    List<Andon> list = JSON.parseArray(andons, Andon.class);
    for (String andonGroup : groupos) {
      for (Andon andon : list) {
        AndonGroupAssign assign = new AndonGroupAssign();
        assign.setSiteCode(getSiteCode());
        assign.setAndonCode(andon.getAndonCode());
        assign.setWorkUnitCode(workUnitCode);
        assign.setAndonGroup(andonGroup);
        try {
          andonGroupAssignService.insertAndonGroupAssign(assign);
        } catch (Exception e) {
          log.error("{}", e);
        }
      }
    }
    return ajaxDone(null);
  }

  /**
   * 
   * <strong>Title : batchOperation<br></strong>
   * <strong>Description : </strong>批量操作（复制/移动）<br> 
   * <strong>Create on : 2016年10月20日 下午3:20:09<br></strong>
   * <p>
   * @param operationType 移动/复制
   * @param sWorkUnitCode 源作业单元
   * @param sAndonGroup 源ANDON组
   * @param andons
   * @return
   * String
   * @throws 
   * @author dapeng.li<br>
   * @version <strong>v1.0.0</strong><br>
   * <br>
   * <strong>修改历史:</strong><br>
   * 修改人 | 修改日期 | 修改描述<br>
   * -------------------------------------------<br>
   * <br>
   * <br>
   */
  @RequestMapping("batchOperation")
  public String batchOperation(Model model, String operationType, String sWorkUnitCode, String sAndonGroup,
      String andons) {
    model.addAttribute("operationType", operationType);
    model.addAttribute("sWorkUnitCode", sWorkUnitCode);
    model.addAttribute("sAndonGroup", sAndonGroup);
    model.addAttribute("andons", andons);
    model.addAttribute("workCentersTree", getWorkCenterTree());
    return "andon/batchOperation";
  }

  @RequestMapping("batchSave")
  @ResponseBody
  public DWZResult batchSave(HttpServletRequest request, String operationType, String sAndonGroup, String tWorkUnitCode,
      String tAndonGroup, String andons) {
    List<Andon> list = JSON.parseArray(andons, Andon.class);
    // 重组数据
    for (Andon andon : list) {
      andon.setSiteCode(getSiteCode());
      setCreateInfo(andon);
      // 源作业单元
      andon.setOriginalWorkUnitCode(andon.getWorkUnitCode());
      // 目的作业单元
      andon.setWorkUnitCode(tWorkUnitCode);
      // 源ANDON组
      if (!StringUtils.isEmpty(sAndonGroup)) {
        andon.setOriginalAndonGroup(sAndonGroup);
      }
      // 目的按灯组
      andon.setAndonGroup(tAndonGroup);
    }

    List<BatchJobReport> reports = new ArrayList<BatchJobReport>();
    if (operationType.equalsIgnoreCase("move")) {
      reports = batchMove(list);
    } else {
      reports = batchCopy(list);
    }
    if (reports.isEmpty()) {
      return ajaxDone(null);
    } else {
      return batchjobAjaxDone(request, reports);
    }

  }

  /**
   * 
   * <strong>Title : batchCopy<br></strong>
   * <strong>Description : </strong>复制操作<br> 
   * <strong>Create on : 2016年10月21日 上午10:16:58<br></strong>
   * <p>
   * @param list
   * @return
   * List<BatchJobReport>
   * @throws 
   * @author dapeng.li<br>
   * @version <strong>v1.0.0</strong><br>
   * <br>
   * <strong>修改历史:</strong><br>
   * 修改人 | 修改日期 | 修改描述<br>
   * -------------------------------------------<br>
   * <br>
   * <br>
   */
  private List<BatchJobReport> batchCopy(List<Andon> list) {
    List<BatchJobReport> reports = new LinkedList<BatchJobReport>();
    int totalError = 0;
    for (Andon andon : list) {
      BatchJobReport report = andonService.doCopyAndon(andon);
      if (!report.isSuccess()) {
        totalError += 1;
      }
      reports.add(report);
    }
    if (totalError == 0) {
      return new ArrayList<BatchJobReport>();
    }
    return reports;
  }

  /**
   * 
   * <strong>Title : batchMove<br></strong>
   * <strong>Description : </strong>移动操作<br> 
   * <strong>Create on : 2016年10月21日 上午10:17:13<br></strong>
   * <p>
   * @param list
   * @return
   * List<BatchJobReport>
   * @throws 
   * @author dapeng.li<br>
   * @version <strong>v1.0.0</strong><br>
   * <br>
   * <strong>修改历史:</strong><br>
   * 修改人 | 修改日期 | 修改描述<br>
   * -------------------------------------------<br>
   * <br>
   * <br>
   */
  private List<BatchJobReport> batchMove(List<Andon> list) {
    List<BatchJobReport> reports = new LinkedList<BatchJobReport>();
    int totalError = 0;
    for (Andon andon : list) {
      // 源作业单元和目的作业单元相同时，只能移动到另外的组或者移出组
      if (andon.getOriginalWorkUnitCode().equals(andon.getWorkUnitCode())) {
        // 删除原组关系
        AndonGroupAssign assign = new AndonGroupAssign();
        assign.setSiteCode(andon.getSiteCode());
        assign.setAndonCode(andon.getAndonCode());
        assign.setWorkUnitCode(andon.getWorkUnitCode());
        assign.setAndonGroup(andon.getOriginalAndonGroup());
        andonGroupAssignService.deleteAndonGroupAssign(assign);
        // 增加新组关系
        if (!StringUtils.isEmpty(andon.getAndonGroup())) {
          assign.setAndonGroup(andon.getAndonGroup());
          try {
            andonGroupAssignService.insertAndonGroupAssign(assign);
          } catch (Exception e) {
            log.error("{}", e);
          }
        }
      } else {
        BatchJobReport report = andonService.doMoveAndon(andon);
        if (!report.isSuccess()) {
          totalError += 1;
        }
        reports.add(report);
      }
    }
    if (totalError == 0) {
      return new ArrayList<BatchJobReport>();
    }
    return reports;
  }

}
